package com.tubang.logistics.service.impl;

import com.github.pagehelper.PageHelper;
import com.tubang.common.util.CollectionUtils;
import com.tubang.logistics.mapper.LogisticsOrderMapper;
import com.tubang.logistics.service.IStatisticsUserService;
import com.tubang.logistics.util.ExcelUtils;
import com.tubang.logistics.vo.statistics.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@Service
public class StatisticcsUserServiceImpl implements IStatisticsUserService {

	@Resource
	private LogisticsOrderMapper logisticsOrderMapper;

	@Override
	public Map<String, Object> listUserPercent(Long startTime, Long endTime, String userId, boolean carrier) {
		Map<String, Object> resultMap = new HashMap<>();
		List<UserPercentVO> userPercentVOList;

		Date startDate = new Date(startTime);
		Date endDate = new Date(endTime);
		userPercentVOList = logisticsOrderMapper.getUserDataByCarrierLimitNine(startDate, endDate, userId, carrier);

		if (!CollectionUtils.isEmpty(userPercentVOList)) {
			//所有订单数量和金额
			OrderDataSimpleVO orderDataSimpleVO = logisticsOrderMapper.countGoodsNumByCarrier(startDate, endDate, userId, carrier);
			BigDecimal otherMoney = orderDataSimpleVO.getOrderMoney();

			BigDecimal otherPercent = new BigDecimal(100);
			Integer otherNum = orderDataSimpleVO.getOrderNum();
			for (UserPercentVO userPercentVO : userPercentVOList) {
				BigDecimal percent = new BigDecimal(userPercentVO.getOrderNum()).divide(
						new BigDecimal(orderDataSimpleVO.getOrderNum()), 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
				otherPercent = otherPercent.subtract(percent);
				otherNum = otherNum - userPercentVO.getOrderNum();
				userPercentVO.setPercent(percent.intValue());
				otherMoney = otherMoney.subtract(userPercentVO.getOrderMoney());
			}

			UserPercentVO userPercentVO = new UserPercentVO();
			userPercentVO.setUserName("其他");
			userPercentVO.setPercent(otherPercent.intValue());
			userPercentVO.setOrderNum(otherNum);
			userPercentVO.setOrderMoney(otherMoney.setScale(2, BigDecimal.ROUND_HALF_UP));
			userPercentVOList.add(userPercentVO);

			resultMap.put("percent", userPercentVOList);
			resultMap.put("goodsTypeNum", orderDataSimpleVO.getOrderNum());
			resultMap.put("productNum", orderDataSimpleVO.getOrderMoney());
		}
		return resultMap;
	}

	@Override
	public List<UserTableVO> listTable(Long startTime, Long endTime, String userId, boolean carrier, String content, Integer page, Integer rows) {
		Map<String, Object> paramMap = new HashMap<>();

		PageHelper.startPage(page, rows);
		paramMap.put("startTime", new Date(startTime));
		paramMap.put("endTime", new Date(endTime));
		paramMap.put("userId", userId);
		paramMap.put("carrier", carrier);
		paramMap.put("content", StringUtils.isNotBlank(content) ? content : null);

		return logisticsOrderMapper.getUserTableByCarrierParam(paramMap);
	}

	@Override
	public HSSFWorkbook exportTable(Long startTime, Long endTime, String userId, boolean carrier, String content) {
		Map<String, Object> paramMap = new HashMap<>();

		paramMap.put("startTime", new Date(startTime));
		paramMap.put("endTime", new Date(endTime));
		paramMap.put("userId", userId);
		paramMap.put("carrier", carrier);
		paramMap.put("content", StringUtils.isNotBlank(content) ? content : null);
		List<UserTableVO> userTableVOS = logisticsOrderMapper.getUserTableByCarrierParam(paramMap);

		//excel内容
		String[][] excelContent = new String[userTableVOS.size() + 1][8];

		//表头
		excelContent[0][0] = "客户名称";
		excelContent[0][1] = "联系电话";
		excelContent[0][2] = "共接单";
		excelContent[0][3] = "已完成单";
		excelContent[0][4] = "未运输";
		excelContent[0][5] = "运输中";
		excelContent[0][6] = "审核中";
		excelContent[0][7] = "异常单";


		//内容
		for (int i = 1; i < userTableVOS.size() + 1; i++) {
			UserTableVO userTableVO = userTableVOS.get(i - 1);
			excelContent[i][0] = userTableVO.getUserName();
			excelContent[i][1] = userTableVO.getMobile();
			excelContent[i][2] = userTableVO.getAllOrderNum() + "/" + userTableVO.getAllWeight().toPlainString();
			excelContent[i][3] = userTableVO.getFinishOrderNum() + "/" + userTableVO.getFinishWeight().toPlainString();
			excelContent[i][4] = userTableVO.getNotSendOrderNum() + "/" + userTableVO.getNotSendWeight().toPlainString();
			excelContent[i][5] = userTableVO.getSendOrderNum() + "/" + userTableVO.getSendWeight().toPlainString();
			excelContent[i][6] = userTableVO.getReviewOrderNum() + "/" + userTableVO.getReviewWeight().toPlainString();
			excelContent[i][7] = userTableVO.getErrorOrderNum() + "/" + userTableVO.getErrorWeight().toPlainString();

		}

		//poi
		return ExcelUtils.createMoreSheetHSSFWorkbook(excelContent);
	}
}
